package com.selfdriving.activity;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.selfdriving.BaseApplication;
import com.selfdriving.R;
import com.selfdriving.adapter.NearFriendsAdapter;
import com.selfdriving.bean.NearFriends;
import com.selfdriving.utils.Constant;
import com.selfdriving.utils.ServiceUtils;
import com.selfdriving.utils.TextUtils;
import com.selfdriving.view.MoMoRefreshListView;
import com.selfdriving.view.MoMoRefreshListView.OnCancelListener;
import com.selfdriving.view.MoMoRefreshListView.OnRefreshListener;

/*** 周边会员 **/
public class WAAmbitusServiceVIP implements OnRefreshListener, OnCancelListener
{
	View view;
	public MoMoRefreshListView listView;
	NearFriendsAdapter adapter;
	BaseApplication application;
	Context context;

	public WAAmbitusServiceVIP(BaseApplication application, Context context)
	{
		// TODO Auto-generated constructor stub
		this.application = application;
		this.context = context;
		LayoutInflater inflater = LayoutInflater.from(context);
		view = inflater.inflate(R.layout.w_activity_near_service_vip, null);
		initViews();
		initEvents();
		getTogetherList();
	}

	public void initViews()
	{
		listView = (MoMoRefreshListView) view
				.findViewById(R.id.w_near_service_hotel_list);
		adapter = new NearFriendsAdapter(application, context,
				application.nearFriendsList);
		listView.setAdapter(adapter);
	}

	protected void initEvents()
	{
		listView.setOnRefreshListener(this);
		listView.setOnCancelListener(this);
		listView.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3)
			{
				if (arg2 < 1)
				{
					return;
				}
				NearFriends nearFriends = (NearFriends) application.nearFriendsList
						.get(arg2 - 1);
				ServiceUtils.post(context, nearFriends.getPhone());
			}
		});
	}

	@Override
	public void onCancel()
	{
		// TODO Auto-generated method stub
		listView.onRefreshComplete();
	}

	@Override
	public void onRefresh()
	{
		getTogetherList();
	}

	public void getTogetherList()
	{
		application.nearFriendsList.clear();
		listView.mState = 2;
		listView.changeHeaderViewByState();
		AjaxParams params = new AjaxParams();
		params.put("f", "55");
		params.put("phone",
				application.user != null ? application.user.getPhone() : "");
		params.put("lat", "" + application.mLatitude);
		params.put("lng", "" + application.mLongitude);
		FinalHttp fh = new FinalHttp();
		fh.configTimeout(10000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				listView.onRefreshComplete();
			}

			@Override
			public void onSuccess(Object o)
			{
				String json = o.toString().trim();
				try
				{
					JSONArray array = new JSONArray(json);
					System.out.println(json);
					NearFriends nearFriends;
					for (int i = 0; i < array.length(); i++)
					{
						JSONObject object = array.getJSONObject(i);
						String phone = object.getString("phone");
						String img = object.getString("img");
						String name = object.getString("name");
						String loginTime = object.getString("loginTime");
						nearFriends = new NearFriends(phone, img, name,
								loginTime);
						application.nearFriendsList.add(nearFriends);
					}
					adapter.notifyDataSetChanged();
					listView.onRefreshComplete();
				} catch (JSONException e)
				{
					e.printStackTrace();
					listView.onRefreshComplete();
				}
			}
		});
	}

	@Override
	public void onLoadMore()
	{
		// TODO Auto-generated method stub
		listView.onLoadMoreComplete(false);
		TextUtils.showCustomToast(context, "没有更多了哟，亲！");
	}

	public View getView()
	{
		return view;
	}
}
